from fbprophet import Prophet
import pandas as pd
import logging
import warnings
import numpy as np

logging.getLogger('fbprophet').setLevel(logging.ERROR)
warnings.filterwarnings("ignore")
df1 = pd.read_csv('./data/sale_all_years.csv', index_col=0, header=0)

df2 = pd.DataFrame(np.zeros((df1.shape[0], 4)))
# for i in range(df1.shape[0]):
#     df = df1.iloc[i, :].T.iloc[3:, ].reset_index()
#     df = df.rename(columns={'index': 'ds', i: 'y'})
#     df['ds'] = df['ds'].apply(lambda x: x[:4] + '-' + x[4:] + '-' + '01')
#     m = Prophet()
#     m.add_seasonality(name='monthly', period=30.5, fourier_order=5)
#     m.add_country_holidays(country_name='CN')
#     m.fit(df)
#     future = m.make_future_dataframe(4, freq='MS')
#     forecast = m.predict(future)
#     y = forecast['yhat']
#     df2.iloc[i, :] = y[-4:]4

ii = 0
def proc(df0):
    df = pd.DataFrame({'ds': df1.columns[2:], 'y': df0[2:]}).reset_index().drop('index', axis=1)
    df['ds'] = df['ds'].apply(lambda x: x[:4] + '-' + x[4:] + '-' + '01')
    global ii
    print('迭代次数:', ii)
    ii += 1
    m = Prophet()
    m.add_seasonality(name='monthly', period=30.5, fourier_order=5)
    m.add_country_holidays(country_name='CN')
    m.fit(df)
    future = m.make_future_dataframe(4, freq='MS')
    forecast = m.predict(future)

    y = forecast['yhat']
    return y[-4:]


df3 = df1.apply(proc, axis=1)
df3.columns = ['201801', '201802', '201803', '201804']
df3.to_csv('data/sales_prophet.csv', index=True, header=True)